package com.igroupes.ovineherd.model.Style;

import com.igroupes.ovineherd.exception.ModelException;
import com.igroupes.ovineherd.model.enums.FormKey;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;

@EqualsAndHashCode(callSuper = true)
@Data
@ToString(callSuper = true)
public class TextStyle extends InputStyle{
    private String regex = ""; //正则表达式
    private String placeholder = ""; //描述字段
    public TextStyle(){
        super();
    }
    public TextStyle(String label, Object value, Boolean required, Integer maxLength, String desc, String regex){
        this(label, value, required, maxLength, desc);
        this.regex = regex;
    }
    public TextStyle(String label, Object value, Boolean required, Integer maxLength, String desc) {
        this(label, value, required, maxLength);
        this.label = label;
        this.disabled = false;
        this.desc = desc;
        this.maxLength = maxLength;
    }
    public TextStyle(String label, Object value, Boolean required, String placeholder, Integer maxLength, String desc) {
        this(label, value, required, maxLength, desc);
        this.placeholder = placeholder;
    }
    private TextStyle(String label, Object value, Boolean required, String desc) {
        this(label, value, required);
        this.desc = desc;
    }
    private TextStyle(String label, Object value, Boolean required, Integer maxLength) {
        this(label, value, required);
        this.maxLength = maxLength;
    }
    public TextStyle(String label, Object value, Boolean required) {
        super(label, value, required);
        this.maxLength = 30;
        this.disabled = false;
        this.type = FormKey.TEXT.getCode();
    }
    public TextStyle(String label, Object value, Boolean required, String desc, boolean disabled, String placeholder) {
        super(label, value, required);
        this.maxLength = 30;
        this.disabled = disabled;
        this.desc = desc;
        this.placeholder = placeholder;
        this.required = false;
        this.type = FormKey.TEXT.getCode();
    }

    @Override
    public Boolean verify(Object o) {
        if(!(o instanceof TextStyle)){
            throw new ModelException("校验style类类型错误");
        }
        TextStyle textStyle = (TextStyle) o;
        if(!textStyle.getDisabled()){
            super.verify(o);
            String value = (String) textStyle.getValue();
            if(textStyle.getRequired() ){
                if(StringUtils.isBlank(value)){
                    throw new ModelException(textStyle.getLabel()+"值是必传字段");
                }
            }
            if(StringUtils.isNotBlank(value)){
                if(!(textStyle.getValue() instanceof String)){
                    throw new ModelException(textStyle.getLabel()+"值必须是字符串");
                }
                if(0 < textStyle.getMaxLength() && value.length() > textStyle.getMaxLength()){
                    throw new ModelException(textStyle.getLabel()+"值最大长度"+textStyle.getMaxLength());
                }
            }
        }
        return true;
    }
}
